package com.cloud.school.app.controller;

import com.alibaba.fastjson.JSON;
import com.cloud.school.app.config.security.SecurityUserUtils;
import com.cloud.school.app.model.MessageModel;
import com.cloud.school.app.service.MessageService;
import com.cloud.school.app.service.impl.ChatWebSocketHandler;
import com.cloud.school.app.util.Pager;
import com.cloud.school.app.util.ResponseUtil;
import com.cloud.school.app.util.enumeration.MessageStatus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName MessageController
 * @Description TODO
 * @Author 36297
 * @Date 2018/12/20 0:13
 * @Version 1.0
 */
@Api(tags = "消息查询控制器")
@RestController
@RequestMapping("/message")
public class MessageController {

    @Autowired
    private MessageService messageService;
    @Autowired
    @Qualifier("chatWebSocketHandler")
    private ChatWebSocketHandler chatWebSocketHandler;

    @ApiOperation("查询用户所有聊天好友记录(每个好友只返回一条,查详情用queryUserChatMessage接口)")
    @RequestMapping(value = "/queryUserMessage", method = RequestMethod.GET)
    public ResponseUtil<Pager<MessageModel>> queryUserMessage(@ApiParam("页数") @RequestParam(defaultValue = "1") int pNo) {
        return ResponseUtil.ok(messageService.selectUserMessage(SecurityUserUtils.getCurrentUserId(), pNo));
    }

    @ApiOperation("根据好友id查询聊天消息")
    @RequestMapping(value = "/queryUserChatMessage", method = RequestMethod.GET)
    public ResponseUtil<Pager<MessageModel>> queryUserChatMessage(@ApiParam("聊天好友id") @RequestParam Long friendUid, @ApiParam("页数") @RequestParam(defaultValue = "1") int pNo) {
        return ResponseUtil.ok(messageService.selectUserMessageByFriendUid(SecurityUserUtils.getCurrentUserId(), friendUid, pNo));
    }

    @ApiOperation("发送消息(不推送,需要前端查询)")
    @RequestMapping(value = "/send", method = RequestMethod.POST)
    public ResponseUtil<Boolean> send(@RequestBody MessageModel messageModel) {
        messageModel.setSendTime(System.currentTimeMillis());
        messageModel.setSendUid(SecurityUserUtils.getCurrentUserId());
        messageModel.setStatus(MessageStatus.UNDELIVERED.getCode());
        return ResponseUtil.ok(messageService.save(messageModel));
    }

    @ApiOperation("发送消息(实时推送,使用ws方式)")
    @RequestMapping(value = "/push", method = RequestMethod.POST)
    public ResponseUtil<Boolean> push(@RequestBody MessageModel messageModel) {
        messageModel.setSendTime(System.currentTimeMillis());
        messageModel.setSendUid(SecurityUserUtils.getCurrentUserId());
        chatWebSocketHandler.saveAndSendMessage(JSON.toJSONString(messageModel));
        return ResponseUtil.ok(true);
    }

    @ApiOperation("查询群组已推送消息")
    @RequestMapping(value = "/queryPushGroupMessage", method = RequestMethod.GET)
    public ResponseUtil<Pager<MessageModel>> queryPushGroupMessage(@ApiParam("接收群组id") @RequestParam Long groupId, @ApiParam("页数") @RequestParam(defaultValue = "1") int pNo) {
        return ResponseUtil.ok(messageService.selectGroupMessageByReceiveId(groupId, MessageStatus.DELIVERED.getCode(), pNo));
    }

    @ApiOperation("查询群组所有消息")
    @RequestMapping(value = "/queryAllGroupMessage", method = RequestMethod.GET)
    public ResponseUtil<Pager<MessageModel>> queryAllGroupMessage(@ApiParam("接收群组id") @RequestParam Long groupId, @ApiParam("页数") @RequestParam(defaultValue = "1") int pNo) {
        return ResponseUtil.ok(messageService.selectGroupMessageByReceiveId(groupId, pNo));
    }

    @ApiOperation("查询聊天室已推送消息(ws)")
    @RequestMapping(value = "/queryPushChatMessage", method = RequestMethod.GET)
    public ResponseUtil<Pager<MessageModel>> queryPushChatMessage(@ApiParam("接收群组id") @RequestParam Long chatRoomId, @ApiParam("页数") @RequestParam(defaultValue = "1") int pNo) {
        return ResponseUtil.ok(messageService.selectChatMessageByReceiveId(chatRoomId, MessageStatus.DELIVERED.getCode(), pNo));
    }

    @ApiOperation("查询聊天室所有消息")
    @RequestMapping(value = "/queryAllChatMessage", method = RequestMethod.GET)
    public ResponseUtil<Pager<MessageModel>> queryAllChatMessage(@ApiParam("接收群组id") @RequestParam Long chatRoomId, @ApiParam("页数") @RequestParam(defaultValue = "1") int pNo) {
        return ResponseUtil.ok(messageService.selectChatMessageByReceiveId(chatRoomId, pNo));
    }
}
